﻿using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace easyccms.General.HtmlHelpers
{
    /// <summary>
    /// Menu helpers that are used for rendering menu.
    /// </summary>
    public static class MenuHelpers
    {

        /// <summary>
        /// Menus the external link.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="linkLocation">The link location.</param>
        /// <returns>Formated link to external web page.</returns>
        public static MvcHtmlString MenuExternalLink(this HtmlHelper htmlHelper, string linkText, string linkLocation)
        {
            TagBuilder link = new TagBuilder("a");
            if (linkLocation.StartsWith("http://")||linkLocation.StartsWith("https://"))
                link.Attributes.Add("href",linkLocation);
            else
            {
                link.Attributes.Add("href",string.Format("http://{0}",linkLocation));
            }

            link.Attributes.Add("alt",linkText);
            link.SetInnerText(linkText);
            return MvcHtmlString.Create(link.ToString());
        }

        /// <summary>
        /// Administrators the sub menu link.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="controllerName">Name of the controller.</param>
        /// <param name="areaName">Name of the area.</param>
        /// <param name="activeMenuClass">The active menu class.</param>
        /// <returns></returns>
        public static MvcHtmlString SubMenuLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, string areaName, string activeMenuClass)
        {
            string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            string currentArea = helper.ViewContext.RouteData.GetRequiredString("area");

            if (controllerName == currentController && actionName == currentAction && areaName == currentArea)
            {
                return helper.ActionLink(
                    linkText,
                    actionName,
                    controllerName,
                    new { area = areaName },
                    new
                    {
                        @class = activeMenuClass
                    });
            }
            return helper.ActionLink(linkText, actionName, controllerName, new { area = areaName }, null);
        }

        /// <summary>
        /// Administrators the menu link.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="controllerName">Name of the controller.</param>
        /// <param name="areaName">Name of the area.</param>
        /// <param name="noSubmenu">if set to <c>true</c> [no submenu].</param>
        /// <param name="activeMenuClass">The active menu class.</param>
        /// <returns></returns>
        public static MvcHtmlString MenuLink(this HtmlHelper helper, string linkText, string controllerName, string areaName, bool noSubmenu, string activeMenuClass)
        {
            string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            string currentArea = helper.ViewContext.RouteData.GetRequiredString("area");

            string cssToApply = "nav-top-item";
            if (noSubmenu == false) cssToApply += " no-submenu";

            if (controllerName == currentController && areaName == currentArea) cssToApply += " current";

            TagBuilder link = new TagBuilder("a");
            link.AddCssClass(cssToApply);
            link.Attributes.Add("href", "#");
            link.SetInnerText(linkText);
            return MvcHtmlString.Create(link.ToString());
        }

        /// <summary>
        /// Administrators the menu link.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="controllerName">Name of the controller.</param>
        /// <param name="areaName">Name of the area.</param>
        /// <param name="noSubmenu">if set to <c>true</c> [no submenu].</param>
        /// <param name="activeMenuClass">The active menu class.</param>
        /// <returns></returns>
        public static MvcHtmlString MenuLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, string areaName, bool noSubmenu, string activeMenuClass)
        {
            string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            string currentArea = helper.ViewContext.RouteData.GetRequiredString("area");

            string cssToApply = "nav-top-item";
            if (noSubmenu == false) cssToApply += " no-submenu";

            if (controllerName == currentController && areaName == currentArea)
            {
                //set as selected
                cssToApply += " current";
            }

            return MvcHtmlString.Create(helper.ActionLink(linkText, actionName, controllerName, new { area = areaName }, new { @Class = cssToApply }).ToHtmlString());
        }
    }
}
